# AMAR ET AL. - COUNTERING MISINFORMATION EARLY (2025)
## REPLICATION FILE: 16_robust_alt_discernment_indx.R
### This script creates alternative discernment indices, excluding two items that might have been discussed in class.
# ----

# Alternative discernment index ----
### Because two discernment questions (exorcism to treat snake bite; and Covid 
### vaccine is safe) might have been discussed in class, we exclude both of them
### in the analyses below.

discernment_dvs_excl_snake <- list(
  list(dv = "accuracy_discernment_excl_snake", label = "Accuracy Discernment", idx = "Accuracy Discernment", subidx = NA, type = "idx", secondary_outcome = FALSE, mechanism = FALSE),
  list(dv = "sharing_discernment_excl_snake", label = "Sharing Discernment", idx = "Sharing Discernment", subidx = NA, type = "idx", secondary_outcome = FALSE, mechanism = FALSE))

discernment_dvs_excl_snake <- bind_rows(discernment_dvs_excl_snake)

regressions_excl_snake <- lapply(discernment_dvs_excl_snake$dv, \(x) tab_regression(x, bimli_svy_dkr.rm)) %>% # make a list of regression tables
  bind_rows %>%
  mutate(margin = qnorm(0.975) * std.error,
         lower = estimate - margin,
         upper = estimate + margin) %>%
  filter(term != "(Intercept)" & !grepl("library_spillover_pre", term)) %>% # remove intercepts and district FEs
  select(-term)
regressions_excl_snake <- left_join(discernment_dvs_excl_snake, regressions_excl_snake, by = "dv")

regressions_excl_snake %>%
  select(label, type, n, estimate, std.error, p.value) %>%
  mutate(type = case_match(type,
                           "idx" ~ "Index"),
         n = format(n, big.mark = ","),
         sig.stars = case_when(p.value < 0.001 ~ "***",
                               p.value < 0.01 ~ "**",
                               p.value < 0.05 ~ "*",
                               TRUE ~ ""),
         estimate = str_c(format(round(estimate, 2), nsmall = 2), sig.stars), # add significance stars to estimate
         std.error = round(std.error, 3),
         p.value = case_when(p.value > 0.9 ~ "$>$0.9",
                             p.value < 0.001 ~ "$<$0.001",
                             p.value < 0.01 ~ as.character(round(p.value, 3)),
                             TRUE ~ as.character(round(p.value, 2)))) %>%
  select(-sig.stars) %>%
  rename(Outcome = label,
         Type = type,
         N = n,
         Estimate = estimate,
         SE = std.error,
         "p-value" = p.value) %>%
  xtable::xtable(caption = "Discernment (excluding 2 items)", align = "lllllll", digits = 3,
                 label = paste0("tab:", "discernment_excl_snake")) %>%
  print(include.rownames = FALSE, 
        caption.placement = "top",
        sanitize.text.function = identity, # add backslashes to special characters
        sanitize.colnames.function = \(x) str_c("\\textbf{", x, "}"),
        add.to.row = list(pos = list(nrow(.)), 
                          command = paste(
                            "\\hline\n\\multicolumn{6}{l}{\\footnotesize *p$<$0.05; **p$<$0.01; ***p$<$0.001. Models include library-spillover strata FEs.} \\\\\n")),
        hline.after = c(-1,0), # add horizontal lane after second to last column
        comment = FALSE,
        table.placement = getOption("xtable.table.placement", "h!"),
        file = paste0("output/tables/tab_unadjusted_", "discernment_excl_snake", ".tex"))

# END of 16_robust_alt_discernment_indx.R ----